<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="类型名称" prop="memberTypeName">
      <el-input v-model="dataForm.memberTypeName" placeholder="卡类型名称"></el-input>
    </el-form-item>
    <el-form-item label="初始积分" prop="initialIntegral">
      <el-input v-model="dataForm.initialIntegral" placeholder="初始积分"></el-input>
    </el-form-item>
    <el-form-item label="等级积分" prop="gradePoints">
      <el-input v-model="dataForm.gradePoints" placeholder="等级积分"></el-input>
    </el-form-item>
    <el-form-item label="折扣" prop="defaultDiscount">
      <el-input-number style="width: 100%;" :min="1" :max="10" v-model="dataForm.defaultDiscount" placeholder="折扣(1-10之间,10标识不打折)"></el-input-number>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-radio v-model="dataForm.status" label="1">启用</el-radio>
       <el-radio v-model="dataForm.status" label="2">停用</el-radio>
    </el-form-item>
	<el-form-item label="办卡是否需要支付费用" prop="payFlag">
	  <el-radio @change="flag(1)" v-model="dataForm.payFlag" label="1">是</el-radio>
	   <el-radio @change="flag(2)" v-model="dataForm.payFlag" label="2">否</el-radio>
	</el-form-item>
	<el-form-item v-show="showpay" label="支付费用" prop="payMoney">
		<el-input v-model="dataForm.payMoney" placeholder="支付费用"></el-input>
	</el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validateNumber = (rule, value, callback) => {
        if (isNaN(value)) {
          callback(new Error('只能为数字'))
        } else {
          callback()
        }
      }
      return {
		showpay:true,
        visible: false,
        dataForm: {
          id: 0,
          memberTypeName: '',
          sellerId: '',
          icon: '',
          backImg: '',
          payFlag: '1',
          initialIntegral: '',
          gradePoints: '',
          defaultDiscount: '',
          detaultTermOfValidity: '',
          status: '1',
          remark: '',
		  payMoney:''
        },
        dataRule: {
          memberTypeName: [
            { required: true, message: '卡类型名称不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '所属商家不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标不能为空', trigger: 'blur' }
          ],
          backImg: [
            { required: true, message: '背景图片不能为空', trigger: 'blur' }
          ],
          payFlag: [
            { required: true, message: '办卡是否需要支付费用不能为空', trigger: 'blur' }
          ],
          initialIntegral: [
            { required: true, message: '初始积分不能为空', trigger: 'blur' },
            { validator: validateNumber, trigger: 'blur' }
          ],
          gradePoints: [
            { required: true, message: '等级积分不能为空', trigger: 'blur' },
            { validator: validateNumber, trigger: 'blur' }
          ],
          defaultDiscount: [
            { required: true, message: '默认折扣不能为空', trigger: 'blur' }
          ],
          detaultTermOfValidity: [
            { required: true, message: '默认有效期不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          payMoney: [
            { validator: validateNumber, trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
	  flag(id){
		  if(id ===1){
			  this.showpay = true
		  }else{
			  this.showpay = false
			  this.dataForm.payMoney = 0
		  }
	  },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoremembertype/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberTypeName = data.tStoreMemberType.memberTypeName
                this.dataForm.sellerId = data.tStoreMemberType.sellerId
                this.dataForm.icon = data.tStoreMemberType.icon
                this.dataForm.backImg = data.tStoreMemberType.backImg
                this.dataForm.payFlag = data.tStoreMemberType.payFlag.toString()
                this.dataForm.initialIntegral = data.tStoreMemberType.initialIntegral
                this.dataForm.gradePoints = data.tStoreMemberType.gradePoints
                this.dataForm.defaultDiscount = data.tStoreMemberType.defaultDiscount
                this.dataForm.detaultTermOfValidity = data.tStoreMemberType.detaultTermOfValidity
                this.dataForm.status = data.tStoreMemberType.status.toString()
                this.dataForm.remark = data.tStoreMemberType.remark
				 this.dataForm.payMoney = data.tStoreMemberType.payMoney
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoremembertype/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'memberTypeName': this.dataForm.memberTypeName,
                'sellerId': this.dataForm.sellerId,
                'icon': this.dataForm.icon,
                'backImg': this.dataForm.backImg,
                'payFlag': this.dataForm.payFlag.toString(),
                'initialIntegral': this.dataForm.initialIntegral,
                'gradePoints': this.dataForm.gradePoints,
                'defaultDiscount': this.dataForm.defaultDiscount,
                'detaultTermOfValidity': this.dataForm.detaultTermOfValidity,
                'status': this.dataForm.status.toString(),
                'remark': this.dataForm.remark,
				'payMoney': this.dataForm.payMoney
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
